<?php

/**
 * slide actions.
 *
 * @package    samsung
 * @subpackage slide
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class slideActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeList(sfWebRequest $request) {
    if (!$request->hasParameter('course_id')) {
      $this->forward404();
    }
    $this->slides = SLIDETable::getInstance()->getByCourseId($request->getParameter('course_id'), 0 , true);
    $this->arrStatusList    = StringUtil::getStatusArray();

    $arrCourseList    = COURSETable::getInstance()->getCoursesArray();
    $this->courseName  = $arrCourseList[$request->getParameter('course_id')];
    $this->courseId    = $request->getParameter('course_id');
  }

  public function executeAdd(sfWebRequest $request) {
    $errorCollector = array();
    $this->form = new SlideForm();
    if ($request->hasParameter('course_id')) {
      $this->form->setDefault(SlideForm::FIELD_COURSE, $request->getParameter('course_id'));
    }
    $this->isEdit = false;
    
    if ($request->hasParameter('id')) {
      $this->isEdit = true;
      $slide = SLIDETable::getInstance()->find($request->getParameter('id'));
      $this->form->populateData($slide);
      if (!$slide instanceof SLIDE) {
        $this->isEdit = false;
      }
    }
    try {
      if ($request->isMethod('post')) {
        $formData = $request->getParameter(SlideForm::FORM_NAME);
        $formFiles = $request->getFiles(SlideForm::FORM_NAME);
        $this->form->bind($formData, $formFiles);

        if ($this->form->isValid()) {
          if (!$this->isEdit) {
            $slide = new SLIDE();
            $lineNo = SLIDETable::getInstance()->getMaxId() + 1;
            $slide->setLineNo($lineNo);
          }
          $slide->setHeadingMain($formData[SlideForm::FIELD_HEADING_MAIN]);
          $slide->setHeadingSub($formData[SlideForm::FIELD_HEADING_SUB]);
          $slide->setDescription($formData[SlideForm::FIELD_DESCRIPTION]);
          $slide->setTemplate($formData[SlideForm::FIELD_TEMPLATE]);
          $slide->setCourseId($formData[SlideForm::FIELD_COURSE]);
          $slide->setIsActive($formData[CourseForm::FIELD_STATUS]);
          $slide->save();

          if (is_uploaded_file($_FILES[SlideForm::FORM_NAME]['tmp_name'][SlideForm::FIELD_IMAGE])) {
            $fileName = $_FILES[SlideForm::FORM_NAME]['name'][SlideForm::FIELD_IMAGE];
            $fileArr  = explode('.', $fileName);
            $extention = $fileArr[count($fileArr)-1];
            $source = $_FILES[SlideForm::FORM_NAME]['tmp_name'][SlideForm::FIELD_IMAGE];
            $destination = sfConfig::get('sf_slide_image_path').$slide->getId().'.'.$extention;

            if (move_uploaded_file($_FILES[SlideForm::FORM_NAME]['tmp_name'][SlideForm::FIELD_IMAGE], $destination)) { error_log('uploaded');
              if (file_exists($destination)) { error_log('resize');
                ImageUtil::resizeImage($destination);
              }
            }
            $slide->setImageExt($extention);
            $slide->save();
          }

          $this->redirect('@slide-list?course_id='.$formData[SlideForm::FIELD_COURSE]);
        }
      }
    } catch (Exception $e) {
      $errorCollector[] = 'Internal error!.. Please try again';
      error_log($e->getMessage());
    }
    $this->errorCollector = $errorCollector;
  }

  public function executeDelete(sfWebRequest $request) {
    $slide = SLIDETable::getInstance()->find($request->getParameter('id'));
    if (!$slide instanceof SLIDE) {
      $this->forward404();
    }
    try {
      $slide->setIsActive(Constants::STATUS_DELETED);
      $slide->save();

      $this->redirect('@slide-list?course_id='.$slide->getCourseId());
    } catch (Exception $e) {

    }
  }
  
}
